package com.army.study.ui;

import com.army.study.ResourceTable;
import com.army.study.utils.LogUtil;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class PopupDialog extends CommonDialog {
    private static final String TAG = PopupDialog.class.getSimpleName();
    // 角半径
    private static final int CORNER_RADIUS = 40;
    // 上下文
    private final Context mContext;

    /**
     * 构造方法
     * @param context
     */
    public PopupDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void create() {
        super.create();
        Component rootView =
                LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_delete, null, false);
        configChoiceButton(rootView);
        configStyle(rootView);
    }

    /**
     * 配置选择按钮点击事件
     * @param rootView
     */
    private void configChoiceButton(Component rootView) {
        LogUtil.info(TAG, "开始配置选择按钮");

        // 获取对话框里Yes文本组件
        Text operateYes = (Text)rootView.findComponentById(ResourceTable.Id_operate_yes);
        // Yes文本组件点击关闭事件
        operateYes.setClickedListener(component -> destroy());
        // 获取对话框里No文本组件
        Text operateNo = (Text)rootView.findComponentById(ResourceTable.Id_operate_no);
        // No文本组件点击关闭事件
        operateNo.setClickedListener(component -> destroy());
    }

    /**
     * 配置样式
     * @param rootView
     */
    private void configStyle(Component rootView) {
        LogUtil.info(TAG, "开始配置对话框样式");

        // 设置对话框大小
        setSize(MATCH_PARENT, MATCH_CONTENT);
        // 显示位置在中间
        setAlignment(LayoutAlignment.CENTER);
        // 角半径，圆角
        setCornerRadius(CORNER_RADIUS);
        // 自动关闭
        setAutoClosable(true);
        // 显示内容组件为rootView
        setContentCustomComponent(rootView);
        // 透明显示
        setTransparent(true);
    }
}
